#!/bin/bash


:<<EOF


  指定 日期( 20231102 )   环境(dev , prod )....
  示例  本地 bash   /home/sky/Desktop/shell/chapter12-project_data_market/load_data_market_application.sh  -t  2024-06-07  -e  local_dev
      测试 bash   /home/sky/Desktop/shell/chapter12-project_data_market/load_data_market_application.sh -t  2023-04-10  -e   dev
      生产 bash   /home/sky/Desktop/shell/chapter12-project_data_market/load_data_market_application.sh  -t  2023-04-10  -e   prod
  bash   /home/sky/Desktop/shell/chapter12-project_data_market/load_data_market_application.sh  -t  2024-06-07  -e   local_dev -f
EOF



readonly  BASE_DIR="$(dirname "$0")"
cd $BASE_DIR || exit;


#---------------------------   导入 脚本  -------------------------------------------#
LOG4S=$BASE_DIR"/""LOG4S.sh"                                  #import  LOG4S.sh  给本次脚本运行上锁
source  "$LOG4S"
LOCK=$BASE_DIR"/""LOCK.sh"                                  #import  LOCK.sh  给本次脚本运行上锁
source  "$LOCK"                                             #先加锁,  防止重复导入
FILE_CONFIGURATION=$BASE_DIR"/""FILE_CONFIGURATION.sh"      #import  FILE_CONFIGURATION.sh
source  "$FILE_CONFIGURATION"                                #文件, 路径, 表, 配置
LOAD_CSV_FUNCTION=$BASE_DIR"/""LOAD_CSV_FUNCTION.sh"         #import  LOAD_CSV_FUNCTION.sh
source  "$LOAD_CSV_FUNCTION"                                 #获取配置后, 读取 数据



#---------------------------   配置 锁  -------------------------------------------#

readonly LOCK_NAME='data_market_load_lock'   #锁的名字不能改动, 否则锁失效
LOCK  $BASE_DIR   $LOCK_NAME


#---------------------------   配置 日志  -----------------------------------------#
readonly  LOGGER_LEVEL=4            #先设置为debug  ， 稳定后再设置为 info : 3
LOG4S_config $LOGGER_LEVEL



#---------------------------   项目默认 配置   --------------------------------------#
readonly DATA_DIR="/dataMarket/"
export TZ=Asia/Shanghai                          #指定时区, 防止 date 调用错误
shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] &&  alias date='gdate'
DATE=$(date -d "yesterday" +%Y-%m-%d)             # 除非指定,  否则默认今天
BRANCH="gx"                                        # 默认gx 这个分支
ENV="dev"                                         # 默认测试环境
FORCE_IMPORT="NO"                                 # TODO  强制导入, 未实现, 预留
FORCE_IMPORT_ARG=""                               # TODO  强制导入, 未实现, 预留
DRY_RUN_ARG=""                                    #  TODO 试运行, Shell之脚本检查与调试参数,  未实现, 预留




#---------------------------   脚本参数 配置   --------------------------------------#
while getopts "e:t:bfD" opt; do
  case ${opt} in
    b )
      BRANCH="$OPTARG"  #  分支参数  预留
      ;;
    e )
      ENV="$OPTARG"     #  环境参数
      ;;
    t )
      DATE="$OPTARG"    # 时间参数
      ;;
    f )
      FORCE_IMPORT="YES"
      FORCE_IMPORT_ARG="--force"
      ;;
    D )
      DRY_RUN_ARG="--dryRun"
      echo "DRYRUN: DRY_RUN_ARG=$DRY_RUN_ARG"
      ;;
    * )
      echo "Invalid option: $opt requires an argument" 1>&2
      ;;
  esac
done
shift $((OPTIND -1))

if [ "${BRANCH}" == "" ]; then
  LOG4S_warn "BRANCH is required. valid options are: gx, gz, hn  ;   If not ,   default  is   gx"
  BRANCH="gx"
fi

LOG4S_debug  "指定环境是$ENV "


if [[ "${ENV}" ==  "" ]] ; then
  LOG4S_warn "ENV   is required. valid options are: dev, prod ;   If not ,   default  is  dev "
  ENV="dev"
fi



if [[ "${DATE}" ==  "" ]] ; then   #如果命令没有指定日期, 那么就获取昨天的数据
  DATE=$(date -d "yesterday" +%Y-%m-%d)
fi
LOG4S_debug  " 导入的日期是    ==> ${DATE}  "         $log_file_path
year_month=$(date -d "$DATE" +%Y%m)   #  yyyy-MM-dd  ==>  yyyymm

year=$(date -d "$DATE" +%Y)           #  yyyy-MM-dd  ==>  yy
month=$(date -d "$DATE" +%m)          #  yyyy-MM-dd  ==> mm
day=$(date -d "$DATE" +%d)            #  yyyy-MM-dd   ==> dd



#---------------------------    开始处理数据  --------------------------------------#


cd    "$DATA_DIR/$ENV/$year_month/$day" || exit    #数据文件路径

echo  " force import ====>  $FORCE_IMPORT "

for   item  in    $(  ls |  grep .txt   )     ;  do
  if [[ "${item}" ==  "all_file_synced.txt"   ]]  && [[  $FORCE_IMPORT == '' ]] ; then
    LOG4S_warn "日期${DATE} 已经同步完毕, 不需要再同步!"
    exit 0
  else
    LOG4S_warn "日期${DATE} 需要强制同步,  无视历史导入!"

  fi
done

parentDir=$(pwd)
NEW_LINE=$'\n'   #new line str
DING_TALK_MSG_STR=''  # DingDing  msg
for   dir  in    $(  ls | grep  -v  .zip | grep -v .txt   )     ;  do   # 对整个目录进行解析 , 过滤掉 zip 和 txt 文件  ；
  echo -e  "\n\n-------------------------------------------------------------------------------------"
  LOG4S_debug "处理的dir ==> ${dir} "
  dir=$parentDir'/'${dir}
  FILE_CONFIGURATION  $dir
  if [[ $LOAD_TABLE == '' ]]; then
      LOG4S_warn "{$dir}  not match  appropriate  table configuration "
      continue
  fi

  file_absolute_path=${dir}'/'$( ls  "$dir" )
  LOG4S_info "处理的文件 ==> $file_absolute_path     行数==>"$(wc -l  $file_absolute_path )
  LOAD_CSV_FUNCTION  ${file_absolute_path}   ${LOAD_TABLE}   ${TABLE_COLUMNS}   ${DATE}    ${ENV}
  DING_TALK_MSG_STR="${DING_TALK_MSG_STR}${NEW_LINE}${LOAD_CSV_FUNCTION_RESP}"
done


LOG4S_warn "导入数据到MariaDB完成, ENV=$ENV, BRANCH=$BRANCH, DATE=$DATE"
cd ..
touch  "all_file_synced.txt"
#---------------------------  TODO 发送 消息(mail || 钉钉 || 微信)   --------------------------------------#
echo "DING_TALK_MSG_STR ==>  ${DING_TALK_MSG_STR} "



